package com.promis.util;

import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

public class DateUtils {
	
	public static String format(Date date, String pattern) {
		if (date == null) return null;
		return new SimpleDateFormat(pattern).format(date);
	}
	
	public static Date parse(String date, String pattern) {
		if (date == null) return null;
    	try {
			return new SimpleDateFormat(pattern).parse(date);
		} catch (ParseException e) {
			return null;
		}  
	}

	public static String format(Date date) {
		return format(date, "yyyy-MM-dd HH:mm:ss.SSS");
	}
	
	public static Date parse(String date) {
		return parse(date, "yyyy-MM-dd HH:mm:ss.SSS");
	}
	
	public static Calendar getCalendar(Date date) {
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		return c;
	}
	
	public static Date getDate(int year, int month, int day) {
		Calendar c = Calendar.getInstance();
		c.set(year, month, day);
		return c.getTime();
	}

	public static int getTotalDayByFristDate(Date startDate){
		long s1 = startDate.getTime();//将时间转为毫秒
		long s2 = new Date().getTime();//得到当前的毫秒
		int day=(int)((s2-s1)/1000/60/60/24);
		return day+1;
	}
	public static int getTotalDayByLastDate(Date endDate){
		endDate =parse(format(endDate),"yyyy-MM-dd");
		long s1 = parse(format(new Date()),"yyyy-MM-dd").getTime();//得到当前的毫秒
		long s2 = endDate.getTime();//将时间转为毫秒
		int day=(int)((s2-s1)/1000/60/60/24);
		return day;
	}
	
	public static int getDaysByYearMonth(int year, int month){
		Calendar c = Calendar.getInstance();
		c.set(year, month,1);
		c.add(Calendar.DAY_OF_YEAR, -1);
		return c.get(Calendar.DAY_OF_MONTH);
	}

	public static String getNearTime(Date dateTime) {
		if (dateTime == null) return "";
		long diff = System.currentTimeMillis() - dateTime.getTime();// 这样得到的差值是微秒级别
		diff = Math.abs(diff);
		long days = diff / (1000 * 60 * 60 * 24);
		long hours = (diff - days * (1000 * 60 * 60 * 24)) / (1000 * 60 * 60);
		long minutes = (diff - days * (1000 * 60 * 60 * 24) - hours * (1000 * 60 * 60)) / (1000 * 60);
		if (days >= 2) return format(dateTime, "yy-MM-dd HH:mm");
		else if (days > 0) return days + "天前";
		else if (hours >= 2) return hours + "小时前";
		else if (hours > 0) return hours + "小时" + minutes + "分前";
		else return (minutes >= 1 ? minutes : 1) + "分前";
	}
	
	public static boolean isNearTime(Date dateTime, Date lastDateTime, long diffMillisecond) {
		if (dateTime == null) return false;
		long diff = dateTime.getTime() - lastDateTime.getTime();// 这样得到的差值是微秒级别
		diff = Math.abs(diff);
		return diffMillisecond > diff;
	}

	public static int getMonth(Date date){
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		int month = calendar.get(Calendar.MONTH) + 1;
		return month;
	}

	public static String formatTime(long time, String pattern) {
		int hours = (int) (time / (1000 * 60 * 60));
		int minute = (int) ((time - hours * (1000 * 60 * 60)) / (1000 * 60));
		int second = (int) ((time - hours * (1000 * 60 * 60) - minute * (1000 * 60)) / 1000);
		int millisecond = (int) ((time - hours * (1000 * 60 * 60) - minute * (1000 * 60) - second * 1000));

		String timeStr = "";
		String tempStr = "";
		if (pattern.contains("HH")) {
			tempStr = "HH";
			timeStr += NumberUtils.formatInteger(hours, 2);
		} else if (pattern.contains("hh")) {
			tempStr = "hh";
			timeStr += NumberUtils.formatInteger(hours, 2);
		}

		if (pattern.contains("mm")) {
			String pattStr = pattern.substring(pattern.indexOf(tempStr) + tempStr.length(), pattern.indexOf("mm"));
			tempStr = "mm";

			timeStr += pattStr + NumberUtils.formatInteger(minute, 2);
		}
		if (pattern.contains("ss")) {
			String pattStr = pattern.substring(pattern.indexOf(tempStr) + tempStr.length(), pattern.indexOf("ss"));
			tempStr = "ss";

			timeStr += pattStr + NumberUtils.formatInteger(second, 2);
		}

		if (pattern.contains("SSS")) {
			String pattStr = pattern.substring(pattern.indexOf(tempStr) + tempStr.length(), pattern.indexOf("SSS"));

			timeStr += pattStr + NumberUtils.formatInteger(millisecond, 2);
		} else if (pattern.contains("SS")) {
			String pattStr = pattern.substring(pattern.indexOf(tempStr) + tempStr.length(), pattern.indexOf("SS"));

			timeStr += pattStr + NumberUtils.formatInteger(millisecond / 10, 2);
		}

		return timeStr;
	}

	//增加天数或减少天数
	public static Date addAndSubtractDaysByCalendar(Date dateTime,int n){

		Calendar calstart = Calendar.getInstance();
		calstart.setTime(dateTime);

		calstart.add(java.util.Calendar.DAY_OF_WEEK, n);

		return calstart.getTime();
	}

	//获取最近12个月
	public static List<String> getTime() {
		List<String> stringList = new ArrayList<String>();
		Calendar calendar = Calendar.getInstance();
		for(int i = 0; i < 12; i++){
			String month = null;
			if(i == 0){
				month = calendar.get(Calendar.MONTH) == 9 ? "10" : calendar.get(Calendar.MONTH) < 9 ? "0" + (calendar.get(Calendar.MONTH) + 1) : "" + (calendar.get(Calendar.MONTH) + 1);
			}else{
				calendar.add(Calendar.MONTH, -1);
				month = calendar.get(Calendar.MONTH) == 9 ? "10" : calendar.get(Calendar.MONTH) < 9 ? "0" + (calendar.get(Calendar.MONTH) + 1) : "" + (calendar.get(Calendar.MONTH) + 1);
			}
			int year = calendar.get(Calendar.YEAR);
			stringList.add(year + "年" + month + "月");
		}
		return stringList;
	}
}
